import { _decorator, Component, Node, Vec2 } from 'cc';
import { GameCtrl } from './GameCtrl';
const { ccclass, property } = _decorator;

@ccclass('MoveBg')
export class MoveBg extends Component {
    @property(Node) img1: Node
    @property(Node) img2: Node

    private moveSpeed: number = 300
    private initPosX: number

    onLoad() {
        this.moveSpeed = GameCtrl.instance.moveSpeed
        this.initPosX = this.img2.position.x
    }

    update(deltaTime: number) {
        const distance = this.moveSpeed * deltaTime
        let pos1 = this.img1.position
        let pos2 = this.img2.position

        this.img1.setPosition(pos1.x - distance, pos1.y)
        this.img2.setPosition(pos2.x - distance, pos2.y)

        if (pos1.x < -730) {
            pos2 = this.img2.position
            this.img1.setPosition(pos2.x + this.initPosX , pos2.y)
        }

        if (pos2.x < -730) {
            pos1 = this.img1.position
            this.img2.setPosition(pos1.x + this.initPosX , pos1.y)
        }
    }
}


